<script lang="ts" setup>
import { ref } from 'vue'

// 定义 props
const props = defineProps<{
  /** 订单ID */
  orderId: string
  /** 金额 */
  amount: number
}>()

// 支付渠道索引
const activeIndex = ref(0)
// 支付渠道
const paymentChannel = [
  {
    title: '微信支付',
    thumb: '/static/images/wechatpay-icon.png',
  },
  // #ifndef MP-WEIXIN
  {
    title: '支付宝支付',
    thumb: '/static/images/alipay-icon.png',
  },
  // #endif
]

// 在线支付弹层
const paymentPopup = ref<UniHelper.UniPopupInstance>()

/** 打开弹层 */
const open = () => {
  paymentPopup.value?.open?.()
}

/** 关闭弹层 */
const close = () => {
  paymentPopup.value?.close?.()
}

// 暴露方法给外部组件使用
defineExpose({
  /** 打开弹层 */
  open,
  /** 关闭弹层 */
  close,
})

// 定义事件
const emits = defineEmits<{
  // 事件名称: [参数1: 类型1, 参数2: 类型2, ... ]
  /** 关闭弹层 */
  close: []
  /** 切换支付渠道 */
  change: [index: number]
  /** 确认支付 */
  confirm: [index: number]
}>()

// 切换支付方式
const handleChange = (index: number) => {
  // 修改高亮
  activeIndex.value = index
  // 触发父组件的 change 事件，将当前 index 传递给父组件
  emits('change', index)
}
</script>

<template>
  <uni-popup :is-mask-click="false" @maskClick="emits('close')" ref="paymentPopup" type="bottom">
    <view class="payment-container">
      <view class="payment-header">
        <text class="title">选择支付方式</text>
        <uni-icons
          @click="emits('close')"
          class="uni-icons-close"
          size="18"
          color="#333"
          type="closeempty"
        />
      </view>
      <view class="order-amount">¥ {{ props.amount }} </view>
      <uni-list :border="false">
        <uni-list-item
          v-for="(item, index) in paymentChannel"
          :key="item.title"
          :title="item.title"
          :thumb="item.thumb"
          clickable
          @click="handleChange(index)"
        >
          <template #footer>
            <uni-icons
              size="26"
              :color="activeIndex === index ? '#16C2A3' : '#d1d1d1'"
              :type="activeIndex === index ? 'checkbox-filled' : 'circle'"
            />
          </template>
        </uni-list-item>
      </uni-list>
      <button class="uni-button" @click="emits('confirm', activeIndex)">立即支付</button>
    </view>
  </uni-popup>
</template>

<style lang="scss">
.payment-container {
  min-height: 400rpx;
  border-radius: 30rpx 30rpx 0 0;
  background-color: #fff;
  padding: 10rpx 30rpx 40rpx;

  .payment-header {
    height: 88rpx;
    line-height: 88rpx;
    text-align: center;
    margin-bottom: 20rpx;
    font-size: 32rpx;
    color: #333;
    position: relative;
  }

  .uni-icons-close {
    position: absolute;
    top: 2rpx;
    right: 0;
  }

  .order-amount {
    padding: 10rpx 0 10rpx;
    text-align: center;
    font-size: 40rpx;
    color: #333;
  }

  :deep(.uni-list-item__container) {
    padding: 40rpx 0 !important;
  }

  :deep(.uni-list-item--hover) {
    background-color: #fff !important;
  }

  :deep(.uni-list-item__icon) {
    margin-right: 0;
  }

  .uni-button {
    margin-top: 40rpx;
  }
}
</style>
